import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:hktk_app/dao/order_dao.dart';
import 'package:hktk_app/ienum/package/order_type_enum.dart';
import 'package:hktk_app/model/package/order_model.dart';

class OrderListPage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() => _createOrderListPageState();
}

class _createOrderListPageState extends State<OrderListPage> {
  String orderStatus = "waitPay";
  String payStatus = "paySuccess";
  double appBarHeight = kToolbarHeight;
  late Future<dynamic> orderList;

  @override
  Widget build(BuildContext context) {
    // 
    return Scaffold(
      appBar: AppBar(
        title: Text("订单列表"),
      ),
      body: Container(
        child: Column(
          children: [
            Container(
              height: 100,
              width: 1.sw - 20,
              padding: EdgeInsets.only(left: 10, right: 10),
              child: Row(spacing: 16, children: [
                DropdownButton(
                  hint: Text("订单状态"),
                  value: orderStatus,
                  items: [
                    DropdownMenuItem(
                      child: Text("等待支付"),
                      value: "waitPay",
                    ),
                    DropdownMenuItem(
                      child: Text("待发货"),
                      value: "waitSend",
                    ),
                    DropdownMenuItem(
                      child: Text("待签收"),
                      value: "waitSign",
                    ),
                    DropdownMenuItem(
                      child: Text("成功"),
                      value: "success",
                    ),
                    DropdownMenuItem(
                      child: Text("已过期"),
                      value: "expired",
                    ),
                  ],
                  onChanged: (String? value) {
                    setState(() {
                      orderStatus = value!;
                      orderList = OrderDao.getOrderList({
                        "orderStatus": orderStatus,
                        "payStatus": payStatus,
                      });
                    });
                  },
                ),
                DropdownButton(
                  value: payStatus,
                  hint: Text("支付状态"),
                  items: [
                    DropdownMenuItem(
                      child: Text("等待支付"),
                      value: "waitPay",
                    ),
                    DropdownMenuItem(
                      child: Text("支付成功"),
                      value: "paySuccess",
                    ),
                  ],
                  onChanged: (String? value) {
                    setState(() {
                      payStatus = value!;
                      orderList = OrderDao.getOrderList({
                        "orderStatus": orderStatus,
                        "payStatus": payStatus,
                      });
                    });
                  },
                )
              ]),
            ),
            FutureBuilder(
                future: orderList,
                builder:
                    (BuildContext context, AsyncSnapshot<dynamic> snapshot) {
                  if (snapshot.connectionState == ConnectionState.done) {
                    var map = snapshot.data! as Map<String, dynamic>;
                    if ((map["records"] as List).length > 0) {
                      List<Widget> widgets = [];
                      (map["records"] as List).forEach((e) {
                        OrderModel order = OrderModel.fromJson(e);
                        widgets.add(Container(
                            padding: EdgeInsets.all(10),
                            decoration: BoxDecoration(
                                // color:Theme.of(context).appBarTheme
                                color: Color.fromARGB(100, 231, 224, 236)),
                            child: Container(
                                decoration: BoxDecoration(
                                  color: Colors.white,
                                  borderRadius: BorderRadius.circular(10),
                                ),
                                child: Column(
                                  mainAxisSize: MainAxisSize.min,
                                  spacing: 5,
                                  children: [
                                    Container(
                                      child: Text(order.packageName!,style: TextStyle(fontSize: 20,fontWeight:FontWeight.bold),),
                                    ),
                                    Container(
                                      width: 1.sw - 30,
                                      child: Row(
                                          mainAxisAlignment:
                                              MainAxisAlignment.spaceBetween,
                                          children: [
                                            Text("创建时间:"),
                                            Text(order.createdTime.toString()),
                                          ]),
                                    ),
                                    // 待支付才显示过期时间
                                    if (order.orderStatus ==
                                        OrderStatusEnum.waitPay)
                                      Container(
                                        width: 1.sw - 30,
                                        child: Row(
                                          mainAxisAlignment:
                                              MainAxisAlignment.spaceBetween,
                                          children: [
                                            Text("过期时间:"),
                                            Text(order.expiredDate.toString())
                                          ],
                                        ),
                                      ),
                                    // Container(
                                    //   width: 1.sw - 30,
                                    //   child: Row(
                                    //       mainAxisAlignment:
                                    //           MainAxisAlignment.spaceBetween,
                                    //       children: [
                                    //         Text("订单类型:"),
                                    //         Text(order.type.toString())
                                    //       ]),
                                    // ),
                                    Container(
                                      width: 1.sw - 30,
                                      child: Row(
                                          mainAxisAlignment:
                                              MainAxisAlignment.spaceBetween,
                                          children: [
                                            Text("价格:"),
                                            Text(
                                                "￥"+(order.price! / 100).toString(),style: TextStyle(color: Colors.red),)
                                          ]),
                                    ),
                                    Container(
                                          alignment:Alignment.centerRight,
                                      child: TextButton(
                                          onPressed: () {
                                          //   前往订单详情页面
                                            Navigator.pushNamed(context, "/mine/orderDetail",arguments: {
                                              "orderId":order.id,
                                              "packageName":order.packageName,
                                            });
                                          },
                                          child: Text(
                                            "详情",
                                            style:
                                                TextStyle(color: Colors.blue),
                                          )),
                                    )
                                  ],
                                ))));
                        return;
                      });
                      return Container(
                          height: 1.sh -
                              appBarHeight -
                              100 -
                              ScreenUtil().statusBarHeight -
                              30,
                          child: ListView(children: widgets));
                    }
                    ;
                  }
                  return Container();
                })
          ],
        ),
      ),
    );
  }

  @override
  void initState() {
    //   initState
    super.initState();
    orderList = OrderDao.getOrderList({
      "orderStatus": orderStatus,
      "payStatus": payStatus,
    });
  }
}
